package com.cskaoyan.service.wx;

import com.cskaoyan.bean.User;
import com.cskaoyan.bean.wx.*;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.mapper.wx.WxCommentMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    WxCommentMapper wxCommentMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public WxCommentListVO queryWxCommentList(Integer valueId, Byte type, Integer size,
                                              Integer page, Integer showType) {
        //分页
        PageHelper.startPage(page, size);

        WxCommentListVO wxCommentListVO = new WxCommentListVO();
        //封装当前页 currentPage
        wxCommentListVO.setCurrentPage(page);
        //封装data
        //showtype=0 全部   showtype =1 有图
        WxCommentExample wxCommentExample = new WxCommentExample();
        if(showType == 0){
            wxCommentExample.createCriteria().andValueIdEqualTo(valueId);
        }else {
            wxCommentExample.createCriteria().andValueIdEqualTo(valueId).andHasPictureEqualTo(true);
        }

        List<WxComment> wxComments = wxCommentMapper.selectByExample(wxCommentExample);
        ArrayList<WxCommentData> wxCommentDataList = new ArrayList<>();

        for (WxComment wxComment : wxComments) {
            //封装data内部的userInfo
            Integer userId = wxComment.getUserId();
            User user = userMapper.selectByPrimaryKey(userId);
            WxUserInfo wxUserInfo = new WxUserInfo();
            wxUserInfo.setAvatarUrl(user.getAvatar());
            wxUserInfo.setNickName(user.getNickname());
            //一定要写在里面，因为ArrayList add()存的是地址，所以每次都要新建一个对象
            WxCommentData wxCommentData = new WxCommentData();
            wxCommentData.setUserInfo(wxUserInfo);
            //封装data其它数据
            wxCommentData.setAddTime(wxComment.getAddTime());
            wxCommentData.setPicList(transfer(wxComment.getPicUrls()));
            wxCommentData.setContent(wxComment.getContent());
            wxCommentDataList.add(wxCommentData);
        }

        wxCommentListVO.setData(wxCommentDataList);
        //封装count
        PageInfo pageInfo = new PageInfo(wxComments);
        long count = pageInfo.getTotal();
        wxCommentListVO.setCount(count);
        return wxCommentListVO;
    }

    @Override
    public Map<String, Integer> queryWxCommentCount(Integer valueId, Byte type) {
        Integer allCount = wxCommentMapper.selectAllCountByValueIdAndType(valueId,type);
        Integer hasPicCount = wxCommentMapper.selectHasPicCountByValueIdAndType(valueId, type);
        Map<String, Integer> map = new HashMap();
        map.put("hasPicCount", hasPicCount);
        map.put("allCount", allCount);
        return map;
    }

    @SneakyThrows
    private String[] transfer(String picUrls) {
        ObjectMapper objectMapper = new ObjectMapper();
        String[] strings = objectMapper.readValue(picUrls, String[].class);
        return strings;
    }
}
